<?php

namespace common\models;

use Yii;
use yii\helpers\ArrayHelper;
use yii\behaviors\TimestampBehavior;

/**
 * This is the model class for table "like".
 *
 * @property int $id
 * @property int $uid
 * @property int $pid
 * @property string $created_at
 */
class Like extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'like';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'uid', 'pid'], 'integer'],
            [['created_at'], 'safe'],
            [['id'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id'         => 'ID',
            'uid'        => 'Uid',
            'pid'        => 'Pid',
            'created_at' => 'Created At',
        ];
    }

    public function beforeSave($insert)
    {
        if (static::findOne(['uid' => $this->uid, 'pid' => $this->pid]) != null) {
            return false;
        }
        if (parent::beforeSave($insert)) {
            if ($insert) {
                $this->created_at = time();
            }

            return true;

        } else {
            return false;
        }
    }
}
